﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using ScormModel.View;
using ScormModel;

namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for SequencingCourseCollectionDialog.xaml
    /// </summary>
    public partial class SequencingCourseCollectionDialog : Window
    {
        ObservableCollection<Sequencing> listSequencing = new ObservableCollection<Sequencing>();
        RibbonBarRegion.RibbonBarHeader parentChild;
        ImsManifest imsmanifest;
        private Organization selectedItem;
        public SequencingCourseCollectionDialog()
        {
            InitializeComponent();
            listPSequencingGrid.ItemsSource = listSequencing;
        }
        public SequencingCourseCollectionDialog(ImsManifest imsmanifest, Organization selectedItem)
        {
            InitializeComponent();
            this.imsmanifest = imsmanifest;
            this.selectedItem = selectedItem;
            for (int i = 0; i < this.imsmanifest.SequencingCollection.ListSequencing.Count; i++)
            {
                this.listSequencing.Add(this.imsmanifest.SequencingCollection.ListSequencing[i]);
            }
            listPSequencingGrid.ItemsSource = listSequencing;
            if (this.selectedItem.Sequencing != null)
            {
                for (int i = 0; i < this.listSequencing.Count; i++)
                {
                    if (this.listSequencing[i].ID == this.selectedItem.Sequencing.Identifier)
                    {
                        listPSequencingGrid.SelectedIndex = i;
                    }
                    else
                        continue;
                }
            }
        }
        private void updateSequencingCollection()
        {
            List<Sequencing> newSequencingList = new List<Sequencing>();
            for (int i = 0; i < this.listSequencing.Count; i++)
            {
                newSequencingList.Add(this.listSequencing[i]);
            }
            this.imsmanifest.SequencingCollection.ListSequencing = newSequencingList;
        }
        public RibbonBarRegion.RibbonBarHeader ParentChild
        {
            get { return this.parentChild; }
            set { this.parentChild = value; }
        }
        public Organization SelectedItem
        {
            get { return this.selectedItem; }
            set { this.selectedItem = value; }
        }
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            List<Sequencing> law = new List<Sequencing>();
            for (int i = 0; i < listSequencing.Count; i++)
            {
                Sequencing qi = listSequencing[i];
                if (i != listPSequencingGrid.SelectedIndex)
                {
                    qi.IsChoose = false;
                    law.Add(qi);
                }
                else
                {
                    qi.IsChoose = true;
                    law.Add(qi);
                }
            }
            listSequencing.Clear();
            for (int i = 0; i < law.Count; i++)
            {
                listSequencing.Add(law[i]);
            }
        }
        private void ViewDetail_Click(object sender, RoutedEventArgs e)
        {
            if (listPSequencingGrid.SelectedIndex >= 0)
            {
                SequencingConfiguration sc = new SequencingConfiguration(listSequencing[listPSequencingGrid.SelectedIndex], listSequencing, listPSequencingGrid.SelectedIndex, listPSequencingGrid);
                sc.Owner = parentChild;
                sc.ShowDialog();
            }
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Sequencing sq = new Sequencing();
            sq.ID = DateTime.Now.Ticks.ToString();
            sq.SequencingName = "New course sequencing";
            sq.Description = "Sequencing description";
            sq.IsEdited = false;
            sq.IsChoose = false;
            listSequencing.Add(sq);
            updateSequencingCollection();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (listPSequencingGrid.SelectedIndex >= 0)
            {
                if (listSequencing[listPSequencingGrid.SelectedIndex].InUse == true)
                {
                    MessageBox.Show("Sequencing is in use");
                    return;
                }
                else
                {
                    listSequencing.RemoveAt(listPSequencingGrid.SelectedIndex);
                }
                updateSequencingCollection();
            }
            else
            {
                MessageBox.Show("Please select sequencing to delete");
            }
        }

        private void btnChoose_Click(object sender, RoutedEventArgs e)
        {
            if (listPSequencingGrid.SelectedIndex >= 0)
            {
                listSequencing[listPSequencingGrid.SelectedIndex].InUse = true;
                // add sequencing to selected item
                Sequencing se = new Sequencing();
                se.Identifier = listSequencing[listPSequencingGrid.SelectedIndex].ID;
                selectedItem.Sequencing = se;
                //assign se
                Close();
            }
            else
            {
                if (listSequencing.Count == 0)
                {
                    MessageBox.Show("Please create a sequencing to assign");
                    return;
                }
                MessageBox.Show("Please select sequencing to assign");
                return;
            }
        }

        private void btnUnChoose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
